package top.yangbuyi.controller.system;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.yangbuyi.acpect.annotaction.Log;
import top.yangbuyi.acpect.enums.BusinessType;
import top.yangbuyi.dto.DeptDto;
import top.yangbuyi.service.impl.DeptService;
import top.yangbuyi.utils.ShiroSecurityUtils;
import top.yangbuyi.vo.AjaxResult;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;


/**
 * @description: 杨不易网站:www.yangbuyi.top
 * @program: yangbuyiopenHis2020
 * @ClassName: DeptController
 * @create: 2020-08-29 19:45
 * @author: yangbuyi
 * @since： JDK1.8
 * @DeptController: 科室控制器$ 科室控制器
 **/

@RestController
@Slf4j
@RequestMapping("system/dept")
public class DeptController {

	  @Autowired
	  private DeptService deptService;

	  /**
	   * @功能描述: 科室分页查询
	   * @Author: TeouBle
	   * @Date: 2020/8/29 19:50
	   */
	  @GetMapping("listForPage")
	  public AjaxResult listForPage(DeptDto deptDto) {
			return AjaxResult.success(deptService.listPage(deptDto));
	  }

	  /**
	   * @功能描述:查询所有可用的科室
	   * @Author: TeouBle
	   * @Date: 2020/8/29 20:02
	   */
	  @GetMapping("selectAllDept")
	  public AjaxResult selectAllDept() {
			return AjaxResult.success(this.deptService.listDept());
	  }

	  /**
	   * @功能描述: 科室添加
	   * @Author: TeouBle
	   * @Date: 2020/8/29 19:50
	   */
	  @PostMapping("addDept")
	  @Log(title = "科室添加", businessType = BusinessType.INSERT)
	  public AjaxResult addDept(@Validated DeptDto deptDto) {
			// 设置添加人员
			deptDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
			return AjaxResult.toAjax(this.deptService.addDept(deptDto));
	  }

	  /**
	   * @功能描述: 科室修改
	   * @Author: TeouBle
	   * @Date: 2020/8/29 19:50
	   */
	  @PutMapping("updateDept")
	  @Log(title = "科室修改", businessType = BusinessType.UPDATE)
	  public AjaxResult updateDept(@Validated DeptDto deptDto) {
			try {
				  // 设置修改人员
				  deptDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
				  return AjaxResult.toAjax(this.deptService.updateDept(deptDto));
			} catch (Exception e) {
				  return AjaxResult.ex(e.getMessage());
			}
	  }

	  /**
	   * @功能描述: 根据ID查询一个科室信息
	   * @Author: TeouBle
	   * @Date: 2020/8/29 20:02
	   */
	  @GetMapping("getDeptByID/{dictid}")
	  @Log(title = "根据ID查询一个科室信息", businessType = BusinessType.SELECT)
	  public AjaxResult getDeptByID(@NotNull(message = "科室ID不能为空") @PathVariable @Validated Long dictid) {
			try {
				  return AjaxResult.success(this.deptService.getOne(dictid));
			}catch (Exception e){
				  return AjaxResult.ex(e.getMessage());
			}
	  }

	  /**
	   * @功能描述:删除
	   * @Author: TeouBle
	   * @Date: 2020/8/29 20:02
	   */
	  @DeleteMapping("deleteDeptByIds/{dictIds}")
	  @Log(title = "根据ID删除科室", businessType = BusinessType.DELETE)
	  public AjaxResult updateDept(@PathVariable @Validated @NotEmpty(message = "要删除的科室ID不能为空") Long[] dictIds) {
			try {
				  return AjaxResult.toAjax(this.deptService.deleteDeptByIds(dictIds));
			}catch (Exception e){
				  return AjaxResult.ex(e.getMessage());
			}
	  }


}
